package jhsys.hithink.basepage.pages;

import java.io.File;

import jhsys.hithink.R;
import jhsys.hithink.basepage.HiApplication;
import jhsys.hithink.basepage.pages.widget.FileBrowser;
import jhsys.hithink.basepage.pages.widget.OnFileBrowserListener;
import jhsys.hithink.dialogs.HiProgressDialog;
import jhsys.hithink.rs.net.FilePath;
import jhsys.hithink.rs.net.FtpUtils;
import jhsys.hithink.rs.net.TcpMessageParser;
import jhsys.hithink.utils.FileUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HiInfoDataMgr extends Activity implements OnClickListener {

	private Button datainput;
	private Button dataoutput;
	private Context mContext;
	private Dialog mProgressDialog;
	private Handler mProgressDialogHandler;
	private static final int DATA_INPUT_DIALOG = 0x0;
	private static final int DATA_OUTPUT_DIALOG = 0x1;
	private static final int DATA_LOAD_DIALOG_SHOW = 0x2;
	private static final int DATA_LOAD_DIALOG_CANCEL = 0x3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.infodata);

		datainput = (Button) findViewById(R.id.datainput);
		dataoutput = (Button) findViewById(R.id.dataoutput);
		datainput.setOnClickListener(this);
		dataoutput.setOnClickListener(this);

		String str = getResources().getString(R.string.data_input_success);
		mProgressDialog = new HiProgressDialog(mContext, str);
		mProgressDialog.setCancelable(false);
		
		mProgressDialogHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case DATA_LOAD_DIALOG_SHOW:
					showDialog(DATA_LOAD_DIALOG_SHOW);
					break;
				case DATA_LOAD_DIALOG_CANCEL:
					mProgressDialog.cancel();
					break;
				}
			}

		};
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.datainput:
			if (HiApplication.isRegistried) {
				showDialog(DATA_INPUT_DIALOG);
			} else {
				Toast.makeText(mContext,
						getResources().getString(R.string.no_register),
						Toast.LENGTH_LONG).show();
			}

			break;
		case R.id.dataoutput:
			if (HiApplication.isRegistried) {
				showDialog(DATA_OUTPUT_DIALOG);
			} else {
				Toast.makeText(mContext,
						getResources().getString(R.string.no_register),
						Toast.LENGTH_LONG).show();
			}
			break;
		default:
			break;
		}

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case DATA_INPUT_DIALOG:
			dialog = getDataInputDialog();
			break;
		case DATA_LOAD_DIALOG_SHOW:
			dialog = mProgressDialog;
			break;
		default:
			dialog = getOutputDialog();
			break;
		}
		return dialog;
	}

	private Dialog getDataInputDialog() {
		Dialog dialog = new InputDialog(mContext);
		return dialog;
	}
	
	class InputDialog extends Dialog {
		public Button confirm, cancel;
		public TextView message;
		public InputDialog(Context context) {
			super(context, R.style.Theme_base_Sence_Execute);
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = (RelativeLayout) inflater.inflate(
					R.layout.exit_dialog, null);
			setContentView(view);
			confirm = (Button) view.findViewById(R.id.confirm);
			cancel = (Button) view.findViewById(R.id.cancel);
			message = (TextView) findViewById(R.id.message);
			confirm.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
					dataInput();
				}
			});
			cancel.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
			message.setText(R.string.datainputquestion);
		}
	}

	private Dialog getOutputDialog() {
		Dialog dialog = new OutputDialog(mContext);
		return dialog;
	}
	
	class OutputDialog extends Dialog {
		public Button confirm, cancel;
		public TextView message;
		public OutputDialog(Context context) {
			super(context, R.style.Theme_base_Sence_Execute);
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = (RelativeLayout) inflater.inflate(
					R.layout.exit_dialog, null);
			setContentView(view);
			confirm = (Button) view.findViewById(R.id.confirm);
			cancel = (Button) view.findViewById(R.id.cancel);
			message = (TextView) findViewById(R.id.message);
			confirm.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
					dataOutput();
				}
			});
			cancel.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
			message.setText(R.string.dataoutputquestion);
		}
	}

	private void dataOutput() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			File file = new File(FilePath.DATA_ZIP_PATH + "HiThink.zip");
			if (file.exists()) {
				FileUtil.delAllFile(FilePath.SDCARD_ZIP_PATH);

				try {
					FileUtil.copyFolder(FilePath.DATA_ZIP_PATH,
							FilePath.SDCARD_ZIP_PATH);
					Toast.makeText(
							mContext,
							getResources().getString(
									R.string.data_output_success),
							Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Toast.makeText(
							mContext,
							getResources().getString(
									R.string.data_output_failed),
							Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}
		} else {
			Toast.makeText(mContext,
					getResources().getString(R.string.no_sdcard),
					Toast.LENGTH_LONG).show();
		}
	}

	private void dataInput() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			File file = new File(FilePath.SDCARD_ZIP_PATH + "HiThink.zip");
			if (file.exists()) {
				try {
					loadDataAgian();

				} catch (Exception e) {
					Toast.makeText(
							mContext,
							getResources()
									.getString(R.string.data_input_failed),
							Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}

		} else {
			Toast.makeText(mContext,
					getResources().getString(R.string.no_sdcard),
					Toast.LENGTH_LONG).show();
		}
	}

	private void loadDataAgian() {
		Thread thread = new Thread() {
			@Override
			public void run() {

				HiApplication.isDownloading = true;
				mProgressDialogHandler.sendEmptyMessage(DATA_LOAD_DIALOG_SHOW);
				FileUtil.delAllFile(FilePath.DATA_ZIP_PATH);
				try {
					FileUtil.copyFolder(FilePath.SDCARD_ZIP_PATH,
							FilePath.DATA_ZIP_PATH);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				FtpUtils.loadDataAgain();
				mProgressDialogHandler
						.sendEmptyMessage(DATA_LOAD_DIALOG_CANCEL);
				HiApplication.isDownloading = false;
				FtpUtils.linkEC1000Again();
				HiApplication.getInstance().cleanExecutedSenceId();
				Intent intent = new Intent(mContext, HiMainActivity.class);
				startActivity(intent);
			}
		};
		thread.start();
	}
}
